package com.anlogic.sdk.openocd.ui;

import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.layout.GridLayoutFactory;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import com.anlogic.sdk.openocd.Activator;
import com.anlogic.sdk.openocd.model.RemoteTarget;
import com.anlogic.sdk.openocd.model.TargetModel;
import com.anlogic.sdk.openocd.preferences.PersistentPreferences;

public class TargetSelectionWidget {
	private Composite container;
	private Combo targetSelectCombo;
	private Button targetCreateButton;
	private Button targetEditButton;
	
	private PersistentPreferences fPersistentPreferences = null;
	private TargetModel model;
	
	public TargetSelectionWidget(Composite parent) {
		loadModel();
		createUI(parent);
	}
	
	private void loadModel() {
		model = new TargetModel();
		fPersistentPreferences = Activator.getInstance().getPersistentPreferences();
		String modelStr = fPersistentPreferences.getWorkspaceString(PersistentPreferences.REMOTE_TARGET_MODEL, "");
		if (modelStr != null && !modelStr.isEmpty()) {
			model = model.readJsonString(modelStr);
		} else {
			RemoteTarget target = new RemoteTarget();
			model.addTarget(target);
		}
	}
	
	private void createUI(Composite parent) {
		Label targetSelectLabel = new Label(parent, SWT.NONE);
		targetSelectLabel.setText("Connection:");
		
		container = new Composite(parent, SWT.DEFER);
		container.setLayout(new GridLayout(3, false));
		container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		GridLayoutFactory.fillDefaults().numColumns(4).margins(0, 0).applyTo(container);
		
		targetSelectCombo = new Combo(container, SWT.POP_UP);
		String[] targetNames = model.getTargetNames();
		targetSelectCombo.setItems(targetNames);
		for (int i = 0; i < targetNames.length; i++) {
			String targetName = targetNames[i];
			if (model.getTarget(targetName).isDefault()) {
				targetSelectCombo.select(i);
			}
		}
		GridDataFactory.fillDefaults().grab(true, false).applyTo((Control) targetSelectCombo);
		
		targetCreateButton = new Button(container, SWT.PUSH);
		targetCreateButton.setText("New");
		targetEditButton = new Button(container, SWT.PUSH);
		targetEditButton.setText("Edit");
		RemoteTarget target = getSelectTarget();
		targetChanged(target);
		
		addListeners();
	}
	
	public Composite getContainer() {
		return this.container;
	}
	
	private void addListeners() {
		targetSelectCombo.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				targetChanged(getSelectTarget());
			}
		});

		targetCreateButton.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				NewTargetDialog dialog = new NewTargetDialog(Display.getCurrent().getActiveShell(), model);
				int ret = dialog.open();
				if (ret == IDialogConstants.OK_ID) {
					fPersistentPreferences.putWorkspaceString(PersistentPreferences.REMOTE_TARGET_MODEL, model.toJsonString());
					String[] targetNames = model.getTargetNames();
					targetSelectCombo.setItems(targetNames);
					RemoteTarget target = dialog.getTarget();
					targetSelectCombo.setText(target.getName());
					targetChanged(target);
					fPersistentPreferences.flush();
				}
			}
		});
		
		targetEditButton.addSelectionListener((SelectionListener) new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				EditTargetDialog dialog = new EditTargetDialog(Display.getCurrent().getActiveShell(), model, getSelectTarget());
				int ret = dialog.open();
				if (ret == IDialogConstants.OK_ID) {
					fPersistentPreferences.putWorkspaceString(PersistentPreferences.REMOTE_TARGET_MODEL, model.toJsonString());
					fPersistentPreferences.flush();
				} else if (ret == EditTargetDialog.REMOVE_ID) {
					fPersistentPreferences.putWorkspaceString(PersistentPreferences.REMOTE_TARGET_MODEL, model.toJsonString());
					String[] targetNames = model.getTargetNames();
					targetSelectCombo.setItems(targetNames);
					RemoteTarget target = model.getDefaultTarget();
					targetSelectCombo.setText(target.getName());
					targetChanged(target);
					fPersistentPreferences.flush();
				}
			}
		});
	}
	
	public void targetChanged(RemoteTarget target) {
		if (target.getName().equals("Local")) {
			targetEditButton.setEnabled(false);
		} else {
			targetEditButton.setEnabled(true);
		}
	}
	
	public void setSelectTargetName(String targetName) {
		this.targetSelectCombo.setText(targetName);
	}
	
	public String getSelectTargetName() {
		return this.targetSelectCombo.getText();
	}
	
	public RemoteTarget getSelectTarget() {
		return model.getTarget(getSelectTargetName());
	}
}
